var expenseTree = {
    treeCategory: function(options, chart, type)
    {
        var checked_ids = [];
        
        $('#btn-tree-cancle').hide();
        $('#btn-tree-save').hide(); 
         
        var tree = $('#tree-category');
        tree.jstree({ 'core' : {
            'data' : {
                'url' : options.url
            },
            'check_callback' : function (operation, node, node_parent, node_position, more) {
                    // operation can be 'create_node', 'rename_node', 'delete_node', 'move_node' or 'copy_node'
                    if (operation === 'delete_node') {
                        var idCheck = node.id.split("_");
                        $.post(options.url_del, { id : idCheck[1] }, function( data ) {
                            if (data === 'TRUE') {
                               tree.jstree("refresh");
                               $('#list').trigger("reloadGrid"); 
                               updateBalance();
                               return true;                              
                            }
                        }); 
                    } else if (operation === 'rename_node') {
                        var idCheck = node.id.split("_");
                 
                        if (idCheck[1]) {
                            $.post(options.url_rename, { id : idCheck[1], name : node_position}, function( data ) {
                                if (data === 'TRUE') {
                                   tree.jstree("refresh"); 
                                   $('#list').trigger("reloadGrid");
                                   $('#list_planned_expenses').trigger("reloadGrid"); 
                                   updateBalance();
                                   return true;
                                }
                            }); 
                        } else {
                            var idParent = node_parent.id.split("_");
                            $.post(options.url_create, { id : idParent[1], name : node_position}, function( data ) {
                                if (data.status) {
                                   tree.jstree("refresh"); 
                                   $('#list').trigger("reloadGrid"); 
                                   $('#list_planned_expenses').trigger("reloadGrid");
                                   updateBalance();
                                   return true;
                                }
                            });
                            return true;
                        }
                    } else if (operation === 'create_node') {
                        $(node).attr("id", "-1")
                        return true;
                    }
                    
                    return false;
                } 
            },
            'checkbox' : {
               "keep_selected_style" : false
            },
            'plugins' : [ "checkbox", "contextmenu" ]/*, contextmenu: {items: customMenu}*/
        });
        
        tree.on('changed.jstree', function (e, data) {
            var i, j  = [];
            checked_ids = [];
            for (i = 0, j = data.selected.length; i < j; i++) {
                var myarr = data.selected[i].split("_");
                checked_ids.push(myarr[1]);
            }
            if (type == 'expenses') {
                var list = $('#list');

                if (data.selected.length > 0) {
                    var date = $('#select-month').val();
                    list.jqGrid('setGridParam',{ postData:{ 'categories': checked_ids.join(','), 'date' : date }, search:true });
                    list.trigger("reloadGrid");
                } else {
                    list[0].clearToolbar();
                }
                updateBalance();
            } else if(type == 'chart') {
                chart.refresh('chart1', checked_ids, options.url_data);
            }
        }).jstree(); 

        
        $('#addMainCategory').click(function() {
            var name = prompt("Please enter category name", "Main category");
            if (name) {
                $.post(options.url_create, { id : null, name : name}, function( data ) {
                    if (data.status) {
                       tree.jstree("refresh"); 
                       $('#list').trigger("reloadGrid"); 
                       $('#list_planned_expenses').trigger("reloadGrid"); 
                       updateBalance();
                       return true;
                    }
                });
            }
        });        
        
        function updateBalance() 
        { 
            var date = $('#select-month').val();
            $.post(options.url_get_total_sum, { date_to : date, date_from : date, categories : checked_ids.join(',') }, function( data ) {         
                $('#total_incomes').text(data.income);
                $('#total_expenses').text(data.expense); 
                var total = data.income - data.expense;
                if (total > 0) {
                    $('#total_balance').css('color', 'green');
                } else {
                    $('#total_balance').css('color', 'red')
                }
                $('#total_balance').text(total);
            });
        }
        
        updateBalance();
        
    }
}
